package com.ulk.systempermission.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.os.PowerManager;

import com.ulk.corelib.db.Packet;
import com.ulk.corelib.utils.log.LogUtils;
import com.ulk.systemset.SystemSetting;

import java.util.List;

/**
 * the power utils class.
 *
 * @author xuxiaohong
 * @date 2016/5/18
 */
public class PowerUtils {

    /**TAG*/
    private static final String TAG = "PowerUtils";

    /**power manager object.*/
    private static PowerManager mPowerManager = null;

    /**system setting object.*/
    private static SystemSetting mSystemSetting = null;

    /**进入深度睡眠*/
    public static void goToDeepSleep(Context context, Packet packet) {
        if (null == context) {
            return;
        }

        long time = System.currentTimeMillis();

        LogUtils.wtf(TAG, "PowerUtils#goToDeepSleep call...");

        // 杀死进程
        String[] keepPackages = null;
        if (null != packet) {
            keepPackages = packet.getStringArray("KeepPackages");
        }
        killProcess(context, keepPackages);

        LogUtils.wtf(TAG, "goToDeepSleep##kill process over, time = " + (System.currentTimeMillis() - time));

//        if (null == mPowerManager) {
//            mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
//        }
//
//        if (null != mPowerManager) {
//            mPowerManager.goToSleep(SystemClock.uptimeMillis());
//        }
//
//        Boolean isRoot = false;
//        ShellUtils.CommandResult sc = null;
//        sc = ShellUtils.execCommand("echo mem > /sys/power/state", isRoot, true);
//        LogUtils.d(TAG, "sc.successMsg = " + sc.successMsg + " sc = errorMsg" + sc.errorMsg + " result = " + sc.result);

        if (null == mSystemSetting) {
            mSystemSetting = new SystemSetting(context);
        }
        mSystemSetting.goToDeepSleep();

        LogUtils.wtf(TAG, "goToDeepSleep##excute over, time = " + (System.currentTimeMillis() - time));
    }

    /**从深度睡眠中唤醒*/
    public static void wakeupFromDeepSleep(Context context, Packet packet) {
        if (null == context) {
            return;
        }

        long time = System.currentTimeMillis();
        LogUtils.wtf(TAG, "PowerUtils#wakeupFromDeepSleep call...");

//        if (null == mPowerManager) {
//            mPowerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
//        }
//        Boolean isRoot = false;
//        ShellUtils.CommandResult sc = null;
//        sc = ShellUtils.execCommand("echo on > /sys/power/state", isRoot, true);
//        LogUtils.d(TAG, "sc.successMsg = " + sc.successMsg + " sc = errorMsg" + sc.errorMsg + " result = " + sc.result);

        if (null == mSystemSetting) {
            mSystemSetting = new SystemSetting(context);
        }
        mSystemSetting.wakeUpFromDeepSleep();
        LogUtils.wtf(TAG, "wakeUpFromDeepSleep##excute over, time = " + (System.currentTimeMillis() - time));
    }

    /**杀死进程*/
    private static void killProcess(Context context, String[] keepPackages) {
        if (null == context) {
            return;
        }

        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (null != am) {
            List<ActivityManager.RunningAppProcessInfo> list = am.getRunningAppProcesses();
            if (null != list) {
                final int size = list.size();
                for (int i = 0;i < size;i++) {
                    ActivityManager.RunningAppProcessInfo temp = list.get(i);
                    if (null != temp) {
                        String[] pkgList = temp.pkgList;
                        if (null != pkgList) {
                            for (int j = 0;j < pkgList.length;j++) {
                                final String p = pkgList[j];
                                if (!isKeepPackage(keepPackages, p)) {
                                    ProcessUtils.killProcess(context, p);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**是否为保留的包名*/
    private static boolean isKeepPackage(String[] keepPackages, String packageName) {
        boolean ret = false;
        if (null != keepPackages && null != packageName) {
            final int length = keepPackages.length;
            for (int i = 0;i < length;i++) {
                if (packageName.equals(keepPackages[i])) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }
}
